﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Skymate.Lyjt
{
    using Abp.Dependency;
    using Abp.Events.Bus;

    using Castle.Core.Logging;

    using Skymate.Commands;
    using Skymate.Lyjt.Core;

    using SuperSocket.SocketBase;
    using SuperSocket.SocketEngine;

    using WeifenLuo.WinFormsUI.Docking;
    
    public partial class Form1 : Form,ISingletonDependency
    {

        private ServerForm sf;

        public Form1()
        {
            InitializeComponent();

            AutoScaleMode = AutoScaleMode.Dpi;
            
            this.dockPanel1.Theme=
                new VS2015LightTheme();
            statusBar.BackColor = this.dockPanel1.Theme.ColorPalette.MainWindowStatusBarDefault.Background;

            this.tssl_version.Text = $"版本号:V{AppVersionHelper.Version}[BuildDate:{AppVersionHelper.ReleaseDate}]";
                //this.tssl_version.Text +"V " +AppVersionHelper.Version;

            EventBus.Default.Register<SessionConnectedEvent>(SessionConnectedEventHandler);
            EventBus.Default.Register<SessionClosedEvent>(SessionClosedEventHandler);

        }

        private void SessionClosedEventHandler(SessionClosedEvent data)
        {
            this.Invoke(new Action(
                () =>
                {
                    var form = this.FindDocument("Client_" + data.Ip);
                    if (form != null)
                    {
                        form.DockHandler.Close();
                    }
                }));
           
        }

        private void SessionConnectedEventHandler(SessionConnectedEvent data)
        {
            this.Invoke(new Action(
               () =>
               {
                   var form = IocManager.Instance.Resolve<ClientForm>(); ;
                   form.ShowHint = DockState.Document;
                   form.Session = data.LightAppSession;
                   form.Name = "Client_" + data.Ip;
                   form.Text = data.Ip;
                   form.Show(this.dockPanel1);

               }));
        }

        /// <summary>
        /// client_ip_port
        /// </summary>
        /// <param name="text"></param>
        /// <returns></returns>
        private IDockContent FindDocument(string name)
        {
            if (dockPanel1.DocumentStyle == DocumentStyle.DockingMdi)
            {
                foreach (Form form in MdiChildren)
                    if (form.Name == name)
                        return form as IDockContent;

            }

            return null;
        }
        
        private void Form1_Load(object sender, EventArgs e)
        {
            sf = new ServerForm();
            sf.Show(this.dockPanel1);
        }

        /// <summary>
        /// 关于 菜单
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void mi_About_Click(object sender, EventArgs e)
        {
            var about=new AboutBox();
            about.ShowDialog();
        }
    }
}
